#include "SelectWidget.h"
#include "Core.h"

Core::Core(Map* theMap, Player* player[], int** memorymap,MouseEvent *mouseEvent)
{
    this->theMap = theMap;  //mainWidget的map对象
    this->player = player;  //所有player的数组
    this->memorymap = memorymap;    //内存图
    this->mouseEvent = mouseEvent;  //点击窗口的鼠标事件
}

void Core::gameUpdate()
{
    //已删除所有不需要的代码！！！

    theMap->clear_CellVisible();     //清空上一帧的视野
    theMap->init_Map_UseToMonitor(); //初始化各ob所处位置的信息地图和需要监视的ob的视野地图

    //Object绘制都在这个函数中！！！
    updateByObject();

    //刷新视野并处理区域探索结果
    theMap->reset_ObjectExploreAndVisible();

    if(mouseEvent->mouseEventType!=NULL_MOUSEEVENT)
    {
        if( mapmoveFrequency == 8) resetNowObject_Click();
        else manageMouseEvent();
    }

}

void Core::updateByObject()
{
    //player管理的各个ob更新状态
    for(int playerIndx = 0; playerIndx < MAXPLAYER ; playerIndx++)
    {
        std::list<Human *>::iterator humaniter=player[playerIndx]->human.begin() , humaniterEnd = player[playerIndx]->human.end();
        list<Building*>::iterator builditer = player[playerIndx]->build.begin() , builditerEnd = player[playerIndx]->build.end();

        //更新human子类的状态
        while(humaniter != humaniterEnd)
        {
            if((*humaniter)->getBlockDR() != INT_MAX && (*humaniter)->getBlockUR() != INT_MAX)
            {
                //更新人物动画
                (*humaniter)->nextframe();
                //绘制
                theMap->add_Map_Object(*humaniter);
                //更新视野
                if(playerIndx == 0) {
                    theMap->reset_CellExplore(*humaniter);
                }
                humaniter++;
            }
        }

        while(builditer != builditerEnd)
        {
            theMap->add_Map_Object(*builditer);
            //更新视野 用户控制的对象
            if(playerIndx == 0){
                theMap->reset_CellExplore(*builditer);
            }
            (*builditer)->nextframe();
            builditer++;
        }
    }

    std::list<Animal*>::iterator animaliter=theMap->animal.begin();
    while(animaliter!=theMap->animal.end())
    {
        (*animaliter)->nextframe();
        theMap->add_Map_Object(*animaliter);
        animaliter++;
    }

    std::list<StaticRes*>::iterator SRiter=theMap->staticres.begin();
    while(SRiter!=theMap->staticres.end())
    {
        theMap->add_Map_Object(*SRiter);
        (*SRiter)->nextframe();
        SRiter++;
    }
}



//处理鼠标事件
void Core::manageMouseEvent()
{
    resetNowObject_Click();
}

void Core::resetNowObject_Click(bool isStop)
{
    if(mouseEvent->mouseEventType==LEFT_PRESS)
    {
        if(isStop)
        {
            call_debugText("blue", " 细节坐标 ("+QString::number(mouseEvent->DR)+","+QString::number(mouseEvent->UR)\
                           +"), 块坐标 (" + QString::number((int)(mouseEvent->DR/BLOCKSIDELENGTH))+","+QString::number((int)(mouseEvent->UR/BLOCKSIDELENGTH))+")", REPRESENT_BOARDCAST_MESSAGE);
        }

        nowobject=g_Object[memorymap[mouseEvent->memoryMapX][mouseEvent->memoryMapY]];
        if(nowobject!=NULL)
            call_debugText("blue"," 点击对象为："+nowobject->getChineseName()+", SN:"+ QString::number(nowobject->getglobalNum()), REPRESENT_BOARDCAST_MESSAGE);
        requestSound_Click(nowobject);
        sel->initActs();
        mouseEvent->mouseEventType=NULL_MOUSEEVENT;
    }
}

void Core::requestSound_Click( Coordinate* object )
{
    if(nowobject == NULL) return;
    Building* buildOb = NULL;
    object->printer_ToBuilding((void**)&buildOb);

    if(buildOb!=NULL && !buildOb->isFinish()) return;

    std::string clickSound = object->getSound_Click();

    if(!clickSound.empty())
        soundQueue.push(clickSound);

    return;
}
